<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
    <van-form @submit="onSubmit">
        <!-- trigger:校验时机，pattern：正则表达式 -->
      <van-field
        v-model="loginForm.mobile"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写用户名',trigger: 'onChange',pattern:/^[1][3,4,5,6,7,8,9][0-9]{9}$/ }]"
      />
      <van-field
        v-model="loginForm.code"
        type="password"
        label="密码"
        required
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码',trigger: 'onChange' ,pattern: /^\d{6}$/}]"
      />
      <div style="margin: 16px;">
        <van-button :loading='isLoding' :disabled="isLoding" round block type="info" native-type="submit" loading-text="登陆中..."
          >登陆</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { Toast } from 'vant'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      loginForm: {
        mobile: '15333731871',
        code: '246810'
      },
      isLoding: false
    }
  },
  methods: {
    ...mapMutations(['getToken']),
    async onSubmit () {
      this.isLoding = true
      try {
        const res = await loginAPI(this.loginForm)
        Toast.success('登陆成功')
        this.getToken(res.data.data)
        this.isLoding = false
        this.$router.replace(this.$route.query.rePage || '/layout')
      } catch (err) {
        console.dir(err)
        Toast.fail('手机号或者密码错误')
        this.isLoding = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background: #007bff;
}

//::v-deep 就是把data-v-hash值选择器写到类名的前面
::v-deep .van-nav-bar__title {
  color: #fff;
}
</style>
